package cn.tedu.boot041.controller;


import cn.tedu.boot041.entity.User;
import cn.tedu.boot041.mapper.UserMappper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Controller
@RestController
public class UserController {

    @Autowired
    private UserMappper mappper;


    @RequestMapping("/reg")
    public String reg(User user) {

        System.out.println("user=" + user);

        User user1 = mappper.selectOne(user.getUsername());
        if (user1 == null) {
            mappper.insert(user);
            return "注册成功!<a href='/login.html'>点击登录</a>";
        }

        return "注册失败!该用户已经存在!";
    }

    @RequestMapping("/login")
    public String login(User user) {
        System.out.println("user=" + user);
        User user1 = mappper.selectOne(user.getUsername());
        System.out.println("user1=" + user1);
        if (user1 != null) {
            if (user.getPassword().equals(user1.getPassword())) {
                return "登录成功!<a href='/index.html'>首页</a>";
            }
            return "登录失败,密码错误";
        }
        return "登录失败,用户不存在";
    }

    @RequestMapping("/selectUsername")
    public String selectUsername(String username) {
        System.out.println("username= " + username);
        User user = mappper.selectOne(username);
        if (user==null){
            return "用户名可用";
        }
        return "该用户名已存在";
    }
}
